package com.facedemo.utils;

import android.graphics.Bitmap;

import java.nio.ByteBuffer;
import java.util.Arrays;

import gu.jimgutil.MatrixUtils;

//摄像头数据转换
public class ConvertDataInfo {
    private int rotate = 0;//图像的旋转角度
    private int picW,picH;//图像的宽高

    private Bitmap bitmapRGB;
    private boolean isBitmapIsRotated = false;//RGB 图像是否已经旋转过了

    private byte[] data_BGR;//转换后得到的 BGR数据
    private int widthBGR,heightBGR;//转换后得到的 BGR图片的宽高

    /**
     * 初始化
     * @param rotate 摄像头返回的图像的旋转角度 0  90  180 270
     * @param picW 图像的宽度
     * @param picH 图像的高度
     * */
    public ConvertDataInfo(int rotate, int picW, int picH) {
        this.rotate = rotate;
        this.picW = picW;
        this.picH = picH;

        data_BGR = new byte[picW * picH * 3];
        widthBGR = picW;
        heightBGR = picH;
    }

    private ByteBuffer byteBuffer;
    /**
     * 数据转换
     * @param cameraData YUV格式的摄像头数据
     * @param isRetainBitmapRGB 是否保留 RGB Bitmap，保留：true，不保留 false
     * */
    public void convert(byte[] cameraData,boolean isRetainBitmapRGB) {
        widthBGR = picW;
        heightBGR = picH;

        //YUV 转 RGB
        bitmapRGB = FastYUVtoRGB.getInstance().convertYUVtoRGB(cameraData, picW, picH);

        int bytes = bitmapRGB.getByteCount();
        if (null == byteBuffer || byteBuffer.capacity() != bytes) {
            byteBuffer = ByteBuffer.allocate(bytes);
        } else {
            byteBuffer.clear();
        }
        bitmapRGB.copyPixelsToBuffer(byteBuffer);
        byte[] temp = byteBuffer.array();

        //RGBA 图像旋转
        if(rotate==90 || rotate==180 || rotate == 270) {
            MatrixUtils.rotate(temp, picW, picH,4,rotate);//参数bpp通道数：BGR旋转填3，RGBA旋转填4
            if(rotate==90 || rotate == 270) {
                int wTemp = widthBGR;
                widthBGR = heightBGR;
                heightBGR = wTemp;
            }
        }

        Arrays.fill(data_BGR,(byte)0);//清空byte[]
        //RGBA 转 BGR
        MatrixUtils.RGBA2BGR(temp, widthBGR, heightBGR,widthBGR*4,data_BGR);

        if(!isRetainBitmapRGB) {
            if (null != bitmapRGB && !bitmapRGB.isRecycled()) {
                bitmapRGB.recycle();
                bitmapRGB = null;
            }
        }
    }

    //获取一份BGR数据的拷贝
    public byte[] getBGRDataCopy() {
        byte[] dataBGR2= new byte[data_BGR.length];
        System.arraycopy(data_BGR, 0, dataBGR2, 0, dataBGR2.length);
        return dataBGR2;
    }

    //获取原版的RGB 现场照片
    public Bitmap getBitmapRGB() {
        if(bitmapRGB == null || bitmapRGB.isRecycled()) return null;
        return bitmapRGB;
    }

    public boolean isBitmapIsRotated() {
        return isBitmapIsRotated;
    }

    //获取旋转正确后的 RGB 现场照片
    public Bitmap getRotatedBitmapRGB(){
        if(bitmapRGB == null || bitmapRGB.isRecycled()) return null;

        if(isBitmapIsRotated) return bitmapRGB;

        if(rotate == 90 || rotate == 270 || rotate == 180){
            bitmapRGB = ImageUtils.rotate(bitmapRGB,rotate,0,0);
            isBitmapIsRotated = true;
        }
        return bitmapRGB;
    }

    //回收 bitmapRGB
    public void recycleRgbBitmap() {
        if (null != bitmapRGB && !bitmapRGB.isRecycled()) {
            bitmapRGB.recycle();
            bitmapRGB = null;
        }
    }

    public byte[] getDataBGR() {
        return data_BGR;
    }

    public int getWidthBGR() {
        return widthBGR;
    }

    public int getHeightBGR() {
        return heightBGR;
    }
}
